<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增物品库存')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:insert="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-record-add">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">购买账户：</label>
                    <div class="col-sm-8">
                        <div class="input-group" style="width: 100%">
                            <select name="account" id="account" class="form-control m-b" >
                                <option value="1">账户1</option>
                                <option value="2">账户2</option>
                                <option value="3">账户3</option>
                                <option value="4">账户4</option>
                                <option value="5">账户5</option>
                                <option value="6">账户6</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">物品名称：</label>
                    <div class="col-sm-8">
                        <div class="input-group" style="width: 100%">
                            <select name="name" id="name" class="form-control m-b" >
                                <option value=""></option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">购买单价：</label>
                    <div class="col-sm-8">
                        <input name="price" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">数量：</label>
                    <div class="col-sm-8">
                        <input name="quantity" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">购买平台：</label>
                    <div class="col-sm-8">
                        <select class="form-control" name="platform">
                            <option value="1">悠悠有品</option>
                            <option value="2">BUFF</option>
                            <option value="3">C5Game</option>
                            <option value="4">Steam</option>
                            <option value="5">IGXE</option>
                            <option value="6">ECO</option>
                        </select>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:insert="include :: select2-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/record"
        $("#form-record-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                let $form = $('#form-record-add');

                // 获取表单数据并修改 price
                let formData = $form.serializeArray();
                let postData = {};

                // 将表单数据转换为对象
                formData.forEach(item => {
                    postData[item.name] = item.value;
                });

                postData.price =  Math.round(parseFloat(postData.price) * 100);

                $.operate.save(prefix + "/add", postData);
            }
        }

        $("input[name='createDate']").datetimepicker({
            format: "yyyy-mm-dd HH:mm:ss",
            minView: 0,
            autoclose: true
        });

        $("input[name='updateDate']").datetimepicker({
            format: "yyyy-mm-dd HH:mm:ss",
            minView: 0,
            autoclose: true
        });

        $(document).ready(function() {
            // 初始化 select2
            $('select[name="name"]').select2({
                placeholder: "请输入物品名称关键词", // 占位符
                allowClear: true, // 允许清空
                width: '100%',    // 宽度
                ajax: {
                    url: '/system/name/list',
                    dataType: 'json',
                    type: "post",
                    delay: 250, // 延迟毫秒数，减少请求频率
                    page: 50,
                    data: function (params) {
                        // 发送到服务器的参数
                        return {
                            name: params.term.split('').join('%') // 用户输入的文本在 `params.term` 中
                        };
                    },
                    processResults: function (data) {
                        // 处理后端返回的数据，转换成 Select2 期望的格式
                        if (data.code === 0) {
                            console.log('搜索数据:', data)
                            var results = $.map(data.rows, function (item) {
                                console.log('选项:', item)
                                return {
                                    id: item.marketHashName,      // 选项的实际值（通常用于表单提交）
                                    text: item.name     // 选项的显示文本
                                }
                            });
                            console.log('选项列表:', results)
                            return {
                                results: results
                            };
                        } else {
                            return { results: [] };
                        }
                    },
                    cache: true
                },
                minimumInputLength: 1 // 开始搜索前所需的最小字符数
            });
        });
    </script>
</body>
</html>